import * as request from '../constant/fetchRequest'
import * as types from '../constant/action-types'
import {getNotificationUnRead} from './notification'
import {message} from 'antd'
import { convertContent } from '../constant/lang-util'
import { getUserOrganizations } from './organization'
import HTTP_CODE from '../constant/httpCode'
export const getUser = user => ({
  type: types.INIT_USER,
  user
})

export const initUser = () => {
  return (dispatch) => {
    (async () => {
      const res = await request.get(`./api/users/21`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(getNotificationUnRead(res.body.id))
        dispatch(getUser(res.body))
        dispatch(getUserOrganizations(res.body.id))
      }
    })()
  }
}

export const searchUserByUsernameOrEmail = (usernameOrEmail, type) => {
  return (dispatch) => {
    (async () => {
      const res = await request.post(`./api/users/searches`, { usernameOrEmail, type })
      if (res.status === HTTP_CODE.OK) {
        dispatch({
          type: types.SEARCH_USER,
          users: res.body
        })
      }
    })()
  }
}

export const addUserToProgram = (programId, userIds) => {
  return (dispatch) => {
    (async () => {
      const res = await request.post(`./api/programs/${programId}/users`, userIds)
      if (res.status === HTTP_CODE.CREATED) {
        dispatch({
          type: types.ADDED_USER,
          users: res.body
        })
      }
    })()
  }
}

export const addTutorToProgram = (programId, userIds) => {
  return (dispatch) => {
    (async () => {
      const res = await request.post(`./api/programs/${programId}/tutors`, userIds)
      if (res.status === HTTP_CODE.CREATED) {
        dispatch({
          type: types.ADDED_USER,
          users: res.body
        })
      }
    })()
  }
}

export const queryProgramById = (programId, callback) => {
  return (dispatch) => {
    (async () => {
      const res = await request.get(`./api/programs/${programId}`)
      if (res.status === HTTP_CODE.OK) {
        callback(res.body)
        dispatch({
          type: types.SEARCH_PROGRAMS,
          program: res.body
        })
      } else {
        callback([])
      }
    })()
  }
}
export const updateRoleChange = (userId, roleNumber, type, usernameOrEmails, organizationId) => {
  return (dispatch) => {
    (async () => {
      const res = await request.update(`./api/users/${userId}/roles`, {userId, roleNumber, type, organizationId})
      if (res.status === HTTP_CODE.NO_CONTENT) {
        message.success(convertContent('编辑成功'))
        dispatch(searchUserRolesByUsernameOrEmail(usernameOrEmails, organizationId))
      }
    })()
  }
}

export const updateUserCurrentOrganizationId = (data, callback) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`./api/users/${data.userId}/organizations`, data)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(getUserById(data.userId))
        callback()
      }
    })()
  }
}
export const searchUserRolesByUsernameOrEmail = (usernameOrEmail, organizationId) => {
  return (dispatch) => {
    (async () => {
      const res = await request.post(`./api/organizations/${organizationId}/users/roles`, { usernameOrEmail })
      if (res.status === HTTP_CODE.OK) {
        dispatch({
          type: types.SEARCH_ROLE_USERS,
          users: res.body
        })
      }
    })()
  }
}
export const getUserById = (id) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`./api/users/${id}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(getUser(res.body))
      }
    })()
  }
}
